package com.scalar.chiptrack.wipProcessView;

import java.util.ArrayList;

/**
 * User: Sujan
 * Date: May 3, 2005
 * Time: 1:01:19 PM
 */
public class NodeList extends ArrayList
{
	public NodeList()
	{
		super();
	}

	public boolean contains ( Object elem )
	{
		Node node = ( Node )elem;
		Node temp = null;
		int size = super.size();
		boolean found = false;
		for ( int i=0; i < size ;i++ )
		{
			temp = ( Node ) super.get ( i );
			if ( temp.getId() == node.getId() )
			{
				found = true;
				break;
			}
		}
		return found;
	}

	public Node addNode ( Node node )
	{
		Node newNode = null;
		if ( !contains( node ) )
		{
			super.add ( node );
			newNode = node;
		}
		else
		{
			newNode = getNodeAt ( getNodeIndexWithId ( node.getId () ) );
		}
		return newNode;
	}
	public int getNodeIndexWithId ( int id )
	{
		int listSize = super.size();
		Node node = null;
		int nodeIndex = -1;
		for ( int i=(listSize-1); i >= 0; i-- )
		{
			node = getNodeAt ( i );
			if ( node.getId () == id )
			{
				nodeIndex = i;
				break;
			}
		}
		return nodeIndex;
	}
	public Node getNodeAt ( int idx )
	{
		int listSize = super.size();
		if ( idx >=0 && idx < listSize )
		{
			return ( Node )super.get ( idx );
		}
		return null;
	}
	public Node getNodeWithId ( int id )
	{
		return getNodeAt ( getNodeIndexWithId ( id ) );
	}
	public NodeList getNodesAtLevel ( int level )
	{
		NodeList levelList = new NodeList();
		int listSize = super.size();
		Node node = null;
		for ( int i=0; i < listSize; i++ )
		{
			node = getNodeAt ( i );
			if ( node.getLevel () == level )
			{
				levelList.addNode ( node );
			}
		}
		return levelList;
	}
}
